package com.shanyue.redwine.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.shanyue.redwine.R;
import com.shanyue.redwine.activity.ActivityWeb;
import com.shanyue.redwine.databinding.FragmentWinecabinetGoodsListBinding;
import com.shanyue.redwine.entity.Page;
import com.shanyue.redwine.entity.WineCabinetGoods;
import com.shanyue.redwine.http.ApiCallBack;
import com.shanyue.redwine.http.ApiResponse;
import com.shanyue.redwine.http.SendBase;
import com.shanyue.redwine.utils.Constants;
import com.shanyue.redwine.utils.RecyclerView.BaseViewHolder;
import com.shanyue.redwine.utils.RecyclerView.CommonAdapter;
import com.shanyue.redwine.utils.SPUtils;
import com.shanyue.redwine.utils.Utils;

import java.util.List;

/**
 * @ 创建 huangxiaoyu
 * @创建时间 2018/8/27 8:29
 * @描述 酒柜商品详情
 */
public class FragmentWineCabinetGoodsList extends FragmentBase<FragmentWinecabinetGoodsListBinding> {

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mPage = new Page();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_winecabinet_goods_list;
    }

    @Override
    protected String getTitle() {
        return null;
    }

    @Override
    protected void initView(Bundle saveInstanceState) {
        mBinding.srlRefresh.setOnRefreshListener(() -> {
            mPage.setRefreshAble(true);
            mPage.setPageIndex(1);
            loadData();
        });
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        mBinding.rvList.setLayoutManager(gridLayoutManager);
        mBinding.rvList.setAdapter(new CommonAdapter<WineCabinetGoods>(getActivity(), R.layout
                .item_winecabinet_goods_list) {
            @Override
            protected void convert(BaseViewHolder holder, WineCabinetGoods item, int position) {
                holder.itemView.setOnClickListener(v -> {
                    Intent intent = new Intent(getActivity(), ActivityWeb.class);
                    intent.setData(Uri.parse("http://114.116.49.3:8001/wap/goods/goodinfo.aspx?id=" + item
                            .getGoods_id() + "&winecabinet=true"));
                    startActivity(intent);
                });
                Utils.loadImg(getActivity(), item.getGoods_images(), holder.getView(R.id.ivIcon));
                holder.setText(R.id.tvName, item.getGoods_name());
                holder.setText(R.id.tvPrice, item.getGoods_money() + "");
            }

        });
        //        mBinding.rvList.addOnScrollListener(new RecyclerView.OnScrollListener() {
        //            @Override
        //            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        //                super.onScrollStateChanged(recyclerView, newState);
        //                LinearLayoutManager manager = (LinearLayoutManager) recyclerView
        // .getLayoutManager();
        //                if (newState == RecyclerView.SCROLL_STATE_IDLE && manager
        //                        .findLastVisibleItemPosition() > recyclerView.getAdapter()
        // .getItemCount()
        //                        - 5 && mPage.isRefreshAble()) {
        //                    mPage.setPageIndex(mPage.getPageIndex() + 1);
        //                    loadData();
        //                }
        //            }
        //        });
        mBinding.srlRefresh.post(() -> loadData());

    }

    void loadData() {
        JSONObject object = new JSONObject();
        object.put("encode", SPUtils.get(getActivity(), Constants.SP_KEY_WINE_CABINET_ID, "").toString());
        SendBase sendBase = new SendBase(getActivity(), object);
        mRestClient.getRectService().getResultString(Constants.URL_WINE_CABINET_GOODS_LIST, JSON.toJSONString
                (sendBase, SerializerFeature.WriteSlashAsSpecial)).enqueue(new ApiCallBack<String>(getActivity()) {
            @Override
            protected void onResponse(ApiResponse<String> response) {
                String result = response.result;
                result.replace("\\", "");
                List<WineCabinetGoods> goods = JSON.parseArray(result, WineCabinetGoods.class);
                ((CommonAdapter) mBinding.rvList.getAdapter()).append(goods);
                mBinding.srlRefresh.setRefreshing(false);
            }
        });

        //        List<WineCabinetGoods> list = new ArrayList<>();
        //        for (int i = 0; i < 20; i++) {
        //            WineCabinetGoods goods = new WineCabinetGoods();
        //            list.add(goods);
        //        }
        //        if (mPage.getPageIndex() > 1) {
        //            ((CommonAdapter) mBinding.rvList.getAdapter()).append(list);
        //        } else {
        //            ((CommonAdapter) mBinding.rvList.getAdapter()).refresh(list);
        //            mBinding.srlRefresh.setRefreshing(false);
        //        }
    }

    public static FragmentWineCabinetGoodsList newInstance() {
        FragmentWineCabinetGoodsList fragment = new FragmentWineCabinetGoodsList();
        return fragment;
    }
}
